package com.monitor.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.monitor.dao.EnvironmentRecordDao;
import com.monitor.po.EnvironmentRecord;
import com.monitor.util.Pager;

@SuppressWarnings("unchecked")
public class EnvironmentRecordDaoImpl implements EnvironmentRecordDao {
	private SessionFactory sessionFactory;

	@Override
	public Pager<EnvironmentRecord> getEnvironmentRecordByUid(long uid, int pageSize, int currentPage) throws Exception {
		try {
			Session sesion = this.sessionFactory.getCurrentSession();
			Query query = sesion.createQuery("select count(*) from EnvironmentRecord e where e.ownerUid=:ownerUid").setLong("ownerUid", uid);
			int totalCount = ((Number) query.uniqueResult()).intValue();
			int pageCount = (totalCount % pageSize == 0) ? totalCount / pageSize : (totalCount / pageSize) + 1;

			List<EnvironmentRecord> list = sesion.createQuery("from EnvironmentRecord e where e.ownerUid=:ownerUid order by e.id desc").setLong("ownerUid", uid)
					.setFirstResult((currentPage - 1) * pageSize).setMaxResults(pageSize).list();
			Pager<EnvironmentRecord> pager = new Pager<EnvironmentRecord>(list, currentPage, totalCount, pageCount);
			return pager;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured when excuting getEnvironmentRecordByUid()....");
		}
	}

	@Override
	public void saveEnvironmentRecord(EnvironmentRecord record) throws Exception {
		try {
			sessionFactory.getCurrentSession().persist(record);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured when excuting saveEnvironmentRecord()....");
		}
	}

	@Override
	public int deleteEnvironmentRecordByUid(long uid) throws Exception {
		try {
			return sessionFactory.getCurrentSession().createQuery("delete from EnvironmentRecord e where e.ownerUid =:ownerUid").setLong("ownerUid", uid).executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured when excuting deleteEnvironmentRecordByUid()....");
		}
	}

	@Override
	public EnvironmentRecord getEnvironmentRecordById(Long id) throws Exception {
		try {
			return (EnvironmentRecord) sessionFactory.getCurrentSession().get(EnvironmentRecord.class, id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured when excuting getEnvironmentRecordById()....");
		}
	}

	@Override
	public void deleteEnvironmentRecord(EnvironmentRecord record) throws Exception {
		try {
			sessionFactory.getCurrentSession().delete(record);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured when excuting deleteEnvironmentRecord()....");
		}
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

}
